package com.hzw.saas.web.app.group.controller;


import javax.validation.Valid;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hzw.saas.api.group.IGroupRoleService;
import com.hzw.saas.api.group.bo.GroupRoleBo;
import com.hzw.saas.api.group.bo.GroupRolePermBo;
import com.hzw.saas.api.group.query.GroupRoleQuery;
import com.hzw.saas.api.group.query.GroupRoleUQuery;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.config.util.PageParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiParam;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author sonam
 * @since 2020-12-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/group/role")
@Api(tags = "群组管理/角色管理")
public class GroupRoleController {

    private final IGroupRoleService roleService;

    @SysLog(operation = "添加角色")
    @PostMapping("/")
    @ApiOperation("添加角色")
    @ApiOperationSort(1)
    public ResponseEntity<GroupRolePermBo> addRole(GroupRoleQuery roleParam) {
        AssertUtil.assertThrow("角色名称不能为空", StringUtil.isBlank(roleParam.getRoleName()));
        return ResponseEntity.ok(roleService.addRole(roleParam));
    }

    @SysLog(operation = "通过id删除角色")
    @DeleteMapping("/{roleId}")
    @ApiOperation("通过id删除角色")
    @ApiOperationSort(2)
    public ResponseEntity<String> deleteRoleById(@ApiParam("角色ID") @PathVariable String roleId) {
        if (roleService.removeBatch(new String[]{roleId})) {
            return ResponseEntity.ok("删除角色成功");
        }
        throw DefaultSaasException.build("删除角色失败").status(HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/{roleId}")
    @ApiOperation("通过id获取角色")
    @ApiOperationSort(3)
    public ResponseEntity<GroupRolePermBo> getRoleById(@ApiParam("角色ID") @PathVariable String roleId) {
        return ResponseEntity.ok(roleService.getRoleById(roleId));
    }

    @SysLog(operation = "更新角色")
    @PutMapping("/")
    @ApiOperation("更新角色")
    @ApiOperationSort(4)
    public ResponseEntity<String> updateRoleById(@Valid GroupRoleUQuery roleUParam) {
        roleService.updateRole(roleUParam);
        return ResponseEntity.ok("修改角色成功");
    }

    @GetMapping("/roles")
    @ApiOperation("获取角色列表")
    @ApiOperationSort(5)
    public ResponseEntity<IPage<GroupRoleBo>> getRoleByPage(PageParam pageParam) {
        return ResponseEntity.ok(roleService.getRoleListByPage(pageParam.convert()));
    }

}
